# Copyright (c) 2009 Olexandr Melnyk <me@omelnyk.net>
# See COPYING for details.

import re
import urllib2

# Translation interface for spanishdict.com
class SpanishDictDictionary:
    # Constructor
    def __init__(self):
        self.short_name = "sd"
        self._word_url = 'http://spanishdict.com/translate/%s'

    # Returns list of supported language pairs
    def supported_pairs(self):
        return [("en", "es"), ("es", "en")]
    
    # Translates word from source_lang to target_lang
    def translate(self, word, source_lang, target_lang):
        html   = urllib2.urlopen(self._word_url % word).read()
        offset = html.find('<p class="d-copyright">')
        html   = html[0:offset + 1]
        
        pattern = '(<span class=\'def\'>)([^<]+)(.*?)(<\/span>)'
        results = re.findall(pattern, html)
        
        return [res[1].strip() for res in results]
    
